#ifndef LOG_STORE_HPP
#define LOG_STORE_HPP

#include "config.h"
#include "utils/singleton.hpp"
#include "utils/log.hpp"

#include <cstdint>

struct DIARY_APP_SDK_API log_item {
    log_item();

    char datetime[32];
    uint64_t  thread_id : 56;
    uint64_t  level     : 8;
    uint16_t  max_payload_size;
    uint16_t  payload_size;
    char payload[0];
};

class DIARY_APP_SDK_API log_store final: public PublicSingleton<log_store> {
    SINGLETON_TYPE();
public:
    class const_iter final {
    public:
        const_iter(uint64_t beg, uint64_t end, uint8_t filter);
        ~const_iter();

        uint64_t count() const;
        const log_item *operator[](uint64_t index);
    private:
        uint64_t beg;
        uint64_t end;
        uint64_t real;
        uint64_t logic;
        uint8_t  filter;
    };

    void init();
    void clean();

    void set_filter(uint8_t level, bool active);
    void reset_filter();
    void set_filter_value(uint8_t mask);
    uint8_t   filter_value() const;

    void push();
    log_item* next() const;
    void clear();
    uint64_t  count() const;

    const_iter logs() const;
};

#endif /* LOG_STORE_HPP */
